package com.taikang.t.webservice.impl;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import com.taikang.common.util.PointUtils;
import com.taikang.common.util.StringUtils;
import com.taikang.common.util.hibernate.SimpleHibernateTemplate;
import com.taikang.t.helper.IKHelpeer;
import com.taikang.t.helper.StanfordParserHelper;
import com.taikang.t.model.BasicWord;
import com.taikang.t.model.FashionWord;
import com.taikang.t.model.FullWord;
import com.taikang.t.model.NormalWord;
import com.taikang.t.model.ProfessionalWord;
import com.taikang.t.model.RandomWord;
import com.taikang.t.webservice.ITalkService;


public class TalkServiceImpl implements
ITalkService {

	private SimpleHibernateTemplate<BasicWord, Long> basicWordDAO;
	private SimpleHibernateTemplate<FashionWord, Long> fashionWordDAO;
	private SimpleHibernateTemplate<FullWord, Long> fullWordDAO;
	private SimpleHibernateTemplate<NormalWord, Long> normalWordDAO;
	private SimpleHibernateTemplate<ProfessionalWord, Long> professionalWordDAO;
	private SimpleHibernateTemplate<RandomWord, Long> randomWordDAO;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		basicWordDAO = new SimpleHibernateTemplate<BasicWord, Long>(sessionFactory, BasicWord.class);
		fashionWordDAO = new SimpleHibernateTemplate<FashionWord, Long>(sessionFactory, FashionWord.class);
		fullWordDAO = new SimpleHibernateTemplate<FullWord, Long>(sessionFactory, FullWord.class);
		normalWordDAO = new SimpleHibernateTemplate<NormalWord, Long>(sessionFactory, NormalWord.class);
		professionalWordDAO = new SimpleHibernateTemplate<ProfessionalWord, Long>(sessionFactory, ProfessionalWord.class);
		randomWordDAO = new SimpleHibernateTemplate<RandomWord, Long>(sessionFactory, RandomWord.class);
	}
	

	public String talk1(String say) {
		// TODO Auto-generated method stub
		//接收到客户的对话
		String pureSay = PointUtils.pointCut(say);
		
		String answer = findFullWord(pureSay);
		
		if(StringUtils.isEmpty(answer))
		{
			Reader reader  = new StringReader(say);
			IKSegmenter ik = new IKSegmenter(reader, true);
			String keyWord = "";
			try {
				while(true){
					Lexeme word = ik.next();
					if(word==null)
					{
						break;
					}
					else
					{
						System.out.println(word);
						if(word.getLexemeText().length()>keyWord.length())
						{
							keyWord = word.getLexemeText();
						}
					}
				}
				answer = findFashionWord(keyWord);
				if(StringUtils.isEmpty(answer))
				{
					answer = findProfessionalWord(keyWord);
					if(StringUtils.isEmpty(answer))
					{
						answer = findNormalWord(keyWord);
						if(StringUtils.isEmpty(answer))
						{
							answer = findBasicWord(keyWord);
							if(StringUtils.isEmpty(answer))
							{
								answer = findRandomWord();
							}
						}
					}
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			return answer;
		}
		
		
		return answer;
	}
	
	
	public static void main(String[] args){
//		talk1("尼玛，坑爹啊有木有");
	}


	@Override
	public String talk(String say) {
		// TODO Auto-generated method stub
		//接收到客户的对话
		String pureSay = PointUtils.pointCut(say);
		String answer = findFullWord(pureSay);
		
		if(StringUtils.isEmpty(answer))
		{
			//先分词
			String sentence = IKHelpeer.distinguish(say);
			//解析语法
			StanfordParserHelper sfph = new StanfordParserHelper(sentence); 
			
			
			String keyWord = sfph.getKeys();
			

				answer = findFashionWord(keyWord);
				if(StringUtils.isEmpty(answer))
				{
					answer = findProfessionalWord(keyWord);
					if(StringUtils.isEmpty(answer))
					{
						answer = findNormalWord(keyWord);
						if(StringUtils.isEmpty(answer))
						{
							answer = findBasicWord(keyWord);
							if(StringUtils.isEmpty(answer))
							{
								answer = findRandomWord();
								if(StringUtils.isEmpty(answer))
								{
									answer = "小T已经无话可说了。。";
								}
							}
						}
					}
				}
				
		}
		else
		{
			return answer;
		}
		
		
		return answer;
	}
	
	
	@SuppressWarnings("unchecked")
	public String findFullWord(String say)
	{
		List answers = fullWordDAO.find("from FullWord where say like '%" + say.trim() + "%'");
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*count);
			FullWord fw = (FullWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}

	
	@SuppressWarnings("unchecked")
	public String findFashionWord(String say)
	{
		List answers = fashionWordDAO.find("from FashionWord where say like '%" + say.trim() + "%'");
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			FashionWord fw = (FashionWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findProfessionalWord(String say)
	{
		List answers = professionalWordDAO.find("from ProfessionalWord where say like '%" + say.trim() + "%'");
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			ProfessionalWord fw = (ProfessionalWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findNormalWord(String say)
	{
		List answers = normalWordDAO.find("from NormalWord where say like '%" + say.trim() + "%'");
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			NormalWord fw = (NormalWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findBasicWord(String say)
	{
		List answers = basicWordDAO.find("from BasicWord where say like '%" + say.trim() + "%'");
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			BasicWord fw = (BasicWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findRandomWord()
	{
		Map fw =  randomWordDAO.findUniqueByNativeSql("select * from T.Random_Word order by rand() fetch first 1 rows only"); 
		if(fw==null)
		{
			return null;
		}
		return (String) fw.get("ANSWER");
	}
}
